﻿namespace MyTRCP.Common.MyProtocol
{
    public class BridgeHelloPayload : BitWalkableObj//bridge->client
    {
        public ushort ClientIdAuthChallengeLength { get; set; }
        public byte[] ClientIdAuthChallenge { get; set; } = [];
        public ushort BridgeIdAuthPublicKeyXLength { get; set; }
        public byte[] BridgeIdAuthPublicKeyX { get; set; } = [];
        public ushort BridgeIdAuthPublicKeyYLength { get; set; }
        public byte[] BridgeIdAuthPublicKeyY { get; set; } = [];
        public ushort BridgeKeyAgreementPublicKeyXLength { get; set; }
        public byte[] BridgeKeyAgreementPublicKeyX { get; set; } = [];
        public ushort BridgeKeyAgreementPublicKeyYLength { get; set; }
        public byte[] BridgeKeyAgreementPublicKeyY { get; set; } = [];
        public BridgeHelloPayload() { }
        public BridgeHelloPayload(byte[] data) : base(data) { }
        public override BitWalker ReadFromBitWalker(BitWalker bw)
        {
            ClientIdAuthChallengeLength = (ushort)bw.ReadBits(16);
            ClientIdAuthChallenge = bw.ReadBytes(ClientIdAuthChallengeLength);
            BridgeIdAuthPublicKeyXLength = (ushort)bw.ReadBits(16);
            BridgeIdAuthPublicKeyX = bw.ReadBytes(BridgeIdAuthPublicKeyXLength);
            BridgeIdAuthPublicKeyYLength = (ushort)bw.ReadBits(16);
            BridgeIdAuthPublicKeyY = bw.ReadBytes(BridgeIdAuthPublicKeyYLength);
            BridgeKeyAgreementPublicKeyXLength = (ushort)bw.ReadBits(16);
            BridgeKeyAgreementPublicKeyX = bw.ReadBytes(BridgeKeyAgreementPublicKeyXLength);
            BridgeKeyAgreementPublicKeyYLength = (ushort)bw.ReadBits(16);
            BridgeKeyAgreementPublicKeyY = bw.ReadBytes(BridgeKeyAgreementPublicKeyYLength);
            return bw;
        }

        public override BitWalker WriteToBitWalker(BitWalker bw)
        {
            return bw
                .WriteBits(ClientIdAuthChallengeLength, 16)
                .WriteBytes(ClientIdAuthChallenge)
                .WriteBits(BridgeIdAuthPublicKeyXLength, 16)
                .WriteBytes(BridgeIdAuthPublicKeyX)
                .WriteBits(BridgeIdAuthPublicKeyYLength, 16)
                .WriteBytes(BridgeIdAuthPublicKeyY)
                .WriteBits(BridgeKeyAgreementPublicKeyXLength, 16)
                .WriteBytes(BridgeKeyAgreementPublicKeyX)
                .WriteBits(BridgeKeyAgreementPublicKeyYLength, 16)
                .WriteBytes(BridgeKeyAgreementPublicKeyY);
        }
    }
}